package cn.highset.modules.base.serviceimpl;

import cn.highset.common.vo.SearchVo;
import cn.highset.modules.base.dao.MiniprogramMenuDao;
import cn.highset.modules.base.entity.MiniprogramMenu;
import cn.highset.modules.base.service.MiniprogramMenuService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 小程序菜单表接口实现
 *
 * @author Victor
 */
@Slf4j
@Service
@Transactional
public class MiniprogramMenuServiceImpl implements MiniprogramMenuService {

    @Autowired
    private MiniprogramMenuDao miniprogramMenuDao;

    @Override
    public MiniprogramMenuDao getRepository() {
        return miniprogramMenuDao;
    }

    @Override
    public Page<MiniprogramMenu> findByCondition(MiniprogramMenu miniprogramMenu, SearchVo searchVo, Pageable pageable) {

        return miniprogramMenuDao.findAll(new Specification<MiniprogramMenu>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<MiniprogramMenu> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");

                List<Predicate> list = new ArrayList<Predicate>();
                //模糊搜素
                if (StrUtil.isNotBlank(miniprogramMenu.getName())) {
                    list.add(cb.like(root.get("name"), '%' + miniprogramMenu.getName() + '%'));
                }
                if (StrUtil.isNotBlank(miniprogramMenu.getPath())) {
                    list.add(cb.like(root.get("path"), '%' + miniprogramMenu.getPath() + '%'));
                }
                //创建时间
                if (StrUtil.isNotBlank(searchVo.getStartDate()) && StrUtil.isNotBlank(searchVo.getEndDate())) {
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }

    @Override
    public List<MiniprogramMenu> getList() {
        return miniprogramMenuDao.findAll(new Specification<MiniprogramMenu>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<MiniprogramMenu> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                List<Predicate> list = new ArrayList<Predicate>();

                list.add(cb.equal(root.get("status"), true));

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                cq.orderBy(cb.asc(root.get("sortOrder")));
                return null;
            }
        });
    }
}
